package xyz.doikki.dkplayer.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;

import xyz.doikki.dkplayer.R;
import xyz.doikki.dkplayer.activity.ui.login.LoginActivity;
import xyz.doikki.dkplayer.fragment.BaseFragment;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private Button loginBtn;
    ActivityResultLauncher<Intent> launcher;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        super.initView();
        launcher = registerForActivityResult(new StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result.getResultCode() == Activity.RESULT_OK){
                    Intent data = result.getData();
                    if (data != null) {
                        String u_name = data.getStringExtra("U_NAME");
                        loginBtn.setText(u_name);
                    }
                }
            }
        });

        loginBtn = findViewById(R.id.btn_login);
        loginBtn.setOnClickListener(this);
        View btn_info1 = findViewById(R.id.btn_info1);
        btn_info1.setOnClickListener(new View.OnClickListener() {
            //匿名内部类
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "app info", Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                launcher.launch(intent);
                break;
        }
    }
}
